<template>
    <div class="container-common homework-publish-outline">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>作业发布与互评管理</el-breadcrumb-item>
            <el-breadcrumb-item>作业概览</el-breadcrumb-item>
        </el-breadcrumb>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    prop="homework"
                    label="作业">
            </el-table-column>
            <el-table-column
                    prop="publishTime"
                    label="布置时间">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="240">
                <template slot-scope="scope">
                    <!--<el-button-->
                            <!--@click="handleEdit(scope.row)"-->
                            <!--type="text"-->
                            <!--size="large">编辑</el-button>-->
                    <el-button
                            type="text"
                            size="large"
                            @click="handleDelete(scope.row)">删除</el-button>
                    <!--<el-button-->
                            <!--type="text"-->
                            <!--size="large"-->
                            <!--@click="handleRead(scope.row)">评阅</el-button>-->
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
                title="提示"
                :visible.sync="deleteDialogVisible"
                width="30%">
            <span v-if="currentData">确认删除作业{{currentData.id}}吗？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelDelete">取 消</el-button>
                <el-button type="primary" @click="confirmDelete">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import Api from '@utils/api';
    import {getDate} from '@utils/utils';
    import { mapGetters } from 'vuex';

    export default {
        name: "outline",
        data () {
            return {
                tableData: [],
                deleteDialogVisible: false,
                currentData: null
            }
        },
        computed: {
            ...mapGetters([
                'getCurrentCourseId'
            ])
        },
        methods: {
            // handleEdit (data) {},
            handleDelete (data) {
                this.currentData = data;
                this.deleteDialogVisible = true;
            },
            // handleRead () {},
            cancelDelete () {
                this.deleteDialogVisible = false;
            },
            confirmDelete () {
                Api({
                    method: 'post',
                    url: '/deleteTask',
                    data: {
                        course_id: this.getCurrentCourseId,
                        task_id: this.currentData.id
                    }
                }).then(() => {
                    this.$notify({
                        type: 'success',
                        title: '删除成功！'
                    });

                    this.cancelDelete();
                    this.getTableData();
                })
            },
            getTableData () {
                Api({
                    method: 'get',
                    url: '/getHomeworkList',
                    params: {
                        course_id: this.getCurrentCourseId
                    }
                }).then(res => {
                    if (res.data && res.data.data && res.data.data.length) {
                        this.tableData = res.data.data.map(item => {
                            return {
                                id: item.task_id,
                                homework: item.task_name,
                                publishTime: getDate(item.start_time)
                            }
                        });
                    }
                })
            }
        },
        created () {
            this.getTableData();
        }
    }
</script>